using System.Linq;
using NUnit.Framework;

namespace Solver
{
	/// <summary>
	/// Evaluate the sum of all amicable pairs under 10000.
	/// </summary>
	public class Problem021 : SelfChecker, IProblem
	{
		private static readonly int[] Primes100 = new[]
		{
			2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101
		};

		public int Id
		{
			get { return 21; }
		}

		public string Solve()
		{
			return Solve(10000).ToString();
		}

		[Test]
		public override void Check()
		{
			Assert.AreEqual(504, Solve(1000));
			Assert.AreEqual(31626, Solve(10000));
		}

		private static int Solve(int param)
		{
			var os = Enumerable.Range(0, param).Select(n => n.SumOfDivisors(Primes100)).ToArray();
			var s = 0;
			for (var n = 1; n < param; n++)
			{
				if (n == os[n]) continue;
				if (os[n] < param && n == os[os[n]]) s += n;
			}
			return s;
		}
	}
}